/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.coupon.entity.Coupon3rd
   Module Description   :

   Date Created      : 2011/7/28
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.coupon.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.GenericGenerator;

import com.yaodian100.core.common.entity.BaseEntity;

/**
 * @author jeffma
 * 
 */
@javax.persistence.Entity
@javax.persistence.Table(name = "EC_3RD_COUPON_MAIN")
public class Coupon3rd extends BaseEntity {

	/** serialVersionUID */
	private static final long serialVersionUID = -1894744658673589758L;

	public enum Range {
		// 整張訂單生效
		Site("全站"),
		// 以"," 分隔活動目錄, 以加入購物車的 z 為準.
		Category("分類"),
		// 活動商品代碼. 限放一個
		Product("商品");
		String desc;

		Range(String desc) {
			this.desc = desc;
		}

		public String getDesc() {
			return desc;
		}

		public String getCode() {
			return name();
		}
	}

	@Id
	@GeneratedValue(generator = "gen_uuid")
	@GenericGenerator(name = "gen_uuid", strategy = "uuid")
	@Column(name = "COUPON_UUID")
	private String uuid;
	@Column(name = "COUPON_NAME", length = 20)
	private String name;
	@Column(name = "COUPON_DESC", length = 500)
	private String description;

	@Column(name = "COUPON_FACE_VALUE")
	private BigDecimal faceValue;

	/** 发放限制条件 付款方式 paymentType */
	@Column(name = "PAYMENT_TYPES", length = 100)
	private String paymentTypes = "";
	/** range, 活动范围 */
	@Column(name = "CAMPAIGN_RANGE", columnDefinition = "numeric(2)", nullable = false)
	private Range range = Range.Site;
	@XmlTransient
	@CollectionOfElements(targetElement = String.class, fetch = FetchType.EAGER)
	@JoinTable(name = "EC_3RD_COUPON_RANGE", joinColumns = @JoinColumn(name = "COUPON_UUID", referencedColumnName = "COUPON_UUID"))
	@Column(name = "RANGE_EXT", length = 30)
	private Set<String> rangeExtList;
	@Column(name = "START_DATE")
	private Date startDate;
	@Column(name = "END_DATE")
	private Date endDate;

	/** 邮件 location */
	@Column(name = "TEMP_LOC", length = 10)
	private String location;
	/** 样板名称 template */
	@Column(name = "TEMP_SMS", updatable = false)
	@Lob
	private String templateSMS;
	/** 样板名称 template */
	@Column(name = "TEMP_MAIL", updatable = false)
	@Lob
	private String templateMail;

	/**
	 * @return the uuid
	 */
	public String getUuid() {
		return uuid;
	}

	/**
	 * @param uuid the uuid to set
	 */
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the faceValue
	 */
	public BigDecimal getFaceValue() {
		return faceValue;
	}

	/**
	 * @param faceValue the faceValue to set
	 */
	public void setFaceValue(BigDecimal faceValue) {
		this.faceValue = faceValue;
	}

	/**
	 * @return the paymentTypes
	 */
	public String getPaymentTypes() {
		return paymentTypes;
	}

	@Transient
	public Set<String> getPaymentTypeArray() {
		Set<String> paymentTypeSet = new HashSet();
		for (String p : StringUtils.split(getPaymentTypes(), ",")) {
			paymentTypeSet.add(p);
		}
		return paymentTypeSet;
	}

	/**
	 * @return the location
	 */
	public String getLocation() {
		return location;
	}

	/**
	 * @param location the location to set
	 */
	public void setLocation(String location) {
		this.location = location;
	}

	/**
	 * @return the templateSMS
	 */
	public String getTemplateSMS() {
		return templateSMS;
	}

	/**
	 * @param templateSMS the templateSMS to set
	 */
	public void setTemplateSMS(String templateSMS) {
		this.templateSMS = templateSMS;
	}

	/**
	 * @return the templateMail
	 */
	public String getTemplateMail() {
		return templateMail;
	}

	/**
	 * @param templateMail the templateMail to set
	 */
	public void setTemplateMail(String templateMail) {
		this.templateMail = templateMail;
	}

	/**
	 * @param paymentTypes the paymentTypes to set
	 */
	public void setPaymentTypes(String paymentTypes) {
		this.paymentTypes = paymentTypes;
	}

	/**
	 * @return the rangeExt
	 */
	@Transient
	public String getRangeExt() {
		return StringUtils.join(getRangeExtList(), ",");
	}

	@XmlTransient
	@Transient
	public List<String> getRangeExtIds() {
		List<String> ids = new ArrayList<String>();
		if (Range.Product.equals(range)) {
			ids.addAll(getRangeExtList());
		} else if (Range.Category.equals(range)) {
			for (String s : getRangeExtList()) {
				String[] c = StringUtils.split(s, "/");
				ids.add(c[c.length - 1]);
			}
		}
		return ids;
	}

	/**
	 * @return the rangeExt
	 */
	public Set<String> getRangeExtList() {
		if (rangeExtList == null) {
			rangeExtList = new HashSet<String>();
		}
		return rangeExtList;
	}

	/**
	 * @param rangeExtList the rangeExtList to set
	 */
	public void setRangeExtList(Set<String> rangeExtList) {
		this.rangeExtList = rangeExtList;
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the range
	 */
	public Range getRange() {
		return range;
	}

	/**
	 * @param range the range to set
	 */
	public void setRange(Range range) {
		this.range = range;
	}
}
